﻿# Create your views here.
from django.template.loader import get_template
from django.template import Context,RequestContext

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.conf import settings

import traceback,sys

#Ajax new implementation
from django.utils import simplejson
import MySQLdb

def get_rec_tab(template_name='rec_table.html'):
    try:
        db = MySQLdb.connect(user = settings.STOCKS_DB_USER, 
                            db = settings.STOCKS_DB_NAME, 
                            passwd = settings.STOCKS_DB_PASS, 
                            host = settings.STOCKS_DB_HOST, 
                            port = settings.STOCKS_DB_PORT)
               
        #PROD
        #'''
        query_select = """
                        select ativo_cod,DATE_FORMAT(data_compra, '%d-%m-%Y %H:%i'),porc_carteira_rec,valor_compra,DATE_FORMAT(data_real_compra, '%d-%m-%Y %H:%i')
                        from recomendacao_instantanea
                        where DATE_FORMAT(data_compra, '%Y-%m-%d') = DATE_FORMAT(CURRENT_DATE(), '%Y-%m-%d')
                        order by data_compra desc
                        """  
        #'''
        #print "query_select: %s" % (query_select)
        cursor = db.cursor()
        cursor.execute(query_select)
        response_dict = {}
        response_dict['response_rec_tab'] = cursor.fetchall()
        response_dict['fields_rec_tab'] = cursor.description
        
        query_select_hist = """
                        select ativo_cod,DATE_FORMAT(data_compra, '%d-%m-%Y %H:%i'),porc_carteira_rec,valor_compra,DATE_FORMAT(data_real_compra, '%d-%m-%Y %H:%i')
                        from recomendacao_instantanea
                        where DATE_FORMAT(data_compra, '%Y-%m-%d') < DATE_FORMAT(CURRENT_DATE(), '%Y-%m-%d')
                        order by data_compra desc
                        LIMIT 7
                        """
        cursor.execute(query_select_hist)
        response_dict['response_rec_tab_hist'] = cursor.fetchall()
        
        response_dict['template_name'] = template_name
        db.close()
        
        return response_dict
    
    except:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                          limit=2, file=sys.stdout) 

def get_rec_tab_ajax_html(request):
    try:
        response_dict=get_rec_tab()
        if response_dict:
            t = get_template(response_dict['template_name'])
            c = RequestContext(request,response_dict)
            html = t.render(c)
            return HttpResponse(html, mimetype='text/html; charset=UTF-8')
        else:
            return HttpResponse(simplejson.dumps('UN'), mimetype='application/json; charset=UTF-8')
    except:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                          limit=2, file=sys.stdout) 

def get_rec_tab_ajax_json(request):
    try:
        #print "ultimo_time:%s" % (ultimo_time)        
        response_dict=get_rec_tab()
        if response_dict:
            return HttpResponse(simplejson.dumps(response_dict['response_rec_inst']), mimetype='application/json; charset=UTF-8')
        else:
            return HttpResponse(simplejson.dumps('UN'), mimetype='application/json; charset=UTF-8')
    except:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                          limit=2, file=sys.stdout) 